﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;

public partial class CCReal : ICPRelationshipPage
{
    public CCData m_ccData = new CCData();
    public CCQuestionData m_ccqData;
    public List<CCQuestionData> m_lstccqData = new List<CCQuestionData>();
    public DataTable m_dtQuestion = new DataTable();

    #region Properties
    public int m_iCCID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ccid"]))
            {
                return Convert.ToInt32(Request.QueryString["ccid"]);
            }
            else
            {
                return -1;
            }
        }
    }
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Courtesy Call - Call Realization";
        if (!IsPostBack && !IsCallback)
        {
            Cached.QuestionTypeTable qttData = new Cached.QuestionTypeTable();
            m_dtQuestion = qttData.GetQuestion();
            InitCallData();
        }

    }
    protected void cbkReal_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleReal(e.Parameter);
    }
	protected void cbkView_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleView(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void InitCallData()
    {
        Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
        DataTable dtCallData = cachedCC.GetCallDetailById(m_iCCID);

        if (dtCallData.Rows.Count > 0)
        {
            m_ccData.iCCID = m_iCCID;
            
            if( dtCallData.Rows[0]["CONTACT_PERSON_ID"] != DBNull.Value )
            {
                m_ccData.iContactPersonID = Convert.ToInt32(dtCallData.Rows[0]["CONTACT_PERSON_ID"]);
            }
            
            if( dtCallData.Rows[0]["NEW_CONTACT_PERSON_ID"] != DBNull.Value )
            {
                m_ccData.iNewContactPersonID = Convert.ToInt32(dtCallData.Rows[0]["NEW_CONTACT_PERSON_ID"]);
            }
            if (dtCallData.Rows[0]["CALL_STATUS"] != DBNull.Value)
            {
                m_ccData.sxCallStatus = dtCallData.Rows[0]["CALL_STATUS"].ToString();
            }
            if (dtCallData.Rows[0]["NOTE"] != DBNull.Value)
            {
                m_ccData.sxNote = dtCallData.Rows[0]["NOTE"].ToString();
            }
            if (dtCallData.Rows[0]["CANCELATION_REASON"] != DBNull.Value)
            {
                m_ccData.sxReason = dtCallData.Rows[0]["CANCELATION_REASON"].ToString();
            }
            if (dtCallData.Rows[0]["PLANNED_DATE"] != DBNull.Value)
            {
                m_ccData.dtmPlannedDate = Convert.ToDateTime(dtCallData.Rows[0]["PLANNED_DATE"]);
            }
            if (dtCallData.Rows[0]["REALIZATION_DATE"] != DBNull.Value)
            {
                m_ccData.dtmRealizationDate = Convert.ToDateTime(dtCallData.Rows[0]["REALIZATION_DATE"]);
            }
            if (dtCallData.Rows[0]["COMPANY_NAME"] != DBNull.Value)
            {
                m_ccData.sxCompanyName = dtCallData.Rows[0]["COMPANY_NAME"].ToString();
            }
            if (dtCallData.Rows[0]["NAME"] != DBNull.Value)
            {
                m_ccData.sxContactPersonName = dtCallData.Rows[0]["NAME"].ToString();
            }
            if (dtCallData.Rows[0]["CUSTOMER_ID"] != DBNull.Value)
            {
                m_ccData.iCustomerID = Convert.ToInt32(dtCallData.Rows[0]["CUSTOMER_ID"]);
            }
            if (dtCallData.Rows[0]["CREATE_BY_NAME"] != DBNull.Value)
            {
                m_ccData.sxCreateBy = dtCallData.Rows[0]["CREATE_BY_NAME"].ToString();
            }
			if (dtCallData.Rows[0]["T_FILE_UPLOAD_ID"] != DBNull.Value)
            {
                m_ccData.iFileUploadId = Convert.ToInt32(dtCallData.Rows[0]["T_FILE_UPLOAD_ID"]);
            }
            if (dtCallData.Rows[0]["ORIGINAL_FILENAME"] != DBNull.Value)
            {
                m_ccData.sxFileName = dtCallData.Rows[0]["ORIGINAL_FILENAME"].ToString();
            }
        }
    }
    private String HandleReal(string sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        InitCallData();
        int iLastQuestionType = -1;
        m_ccqData = null;
        m_ccData.iCCID = m_iCCID;
        m_ccData.sxCallStatus = "REALIZED";
        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            switch (sxRowSplitted[0].ToUpper())
            {
                case KCC.REALIZATION_DATE:
                    m_ccData.dtmRealizationDate = Convert.ToDateTime(sxRowSplitted[1]);
                    break;
				case KCC.FILEUPLOADID:
                    if(!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        m_ccData.iFileUploadId = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    break;
                case KCC.NOTE:
                    m_ccData.sxNote = sxRowSplitted[1].ToString();
                    break;
                default:
                    int iCurrentQuestionType = Convert.ToInt32(sxRowSplitted[0].Split('_')[0]);

                    if (iLastQuestionType != iCurrentQuestionType)
                    {
                        if (iLastQuestionType > 0)
                        {
                            m_lstccqData.Add(m_ccqData);
                        }

                        m_ccqData = new CCQuestionData();
                        m_ccqData.iCCID = m_iCCID;
                        m_ccqData.iQuestionTypeID = Convert.ToInt32(sxRowSplitted[0].Split('_')[0]);
                        m_ccqData.sxValue = sxRowSplitted[1].ToString();
                        iLastQuestionType = iCurrentQuestionType;
                    }
                    else
                    {
                        if (sxRowSplitted[0].ToString().Contains("_note"))
                        {
                            m_ccqData.sxNote = sxRowSplitted[1].ToString();
                        }
                    }
                    break;
            }
        }

        if (m_ccqData != null)
        {
            m_lstccqData.Add(m_ccqData);
        }

        return DoRealization();
        
    }
    private String DoRealization()
    {
        DBLib dbl = new DBLib();
        dbl.BeginTransaction();
        try
        {
            DBQuery.CourtesyCallTable.UpdateCallData(dbl, m_ccData);
            foreach (CCQuestionData ccqdata in m_lstccqData)
            {
                ccqdata.iCCQuestionID = DBQuery.CourtesyCallTable.SaveCCQuestion(dbl, ccqdata);
                if (!String.IsNullOrEmpty(ccqdata.sxValue))
                {
                    DBQuery.CourtesyCallTable.CopyCCQuestion2CustInfo(dbl, m_ccData.iCustomerID, ccqdata);
                }
            }
            dbl.EndTransaction(true);
            Cached.CourtesyCallTable cachedCC = new Cached.CourtesyCallTable();
            cachedCC.RemoveCache();
        }
        catch (Exception ex)
        {
            dbl.EndTransaction(false);
            throw ex;
        }

        //insert into newsfeed
        NewsFeedData nfData = new NewsFeedData();
        nfData.iCustomerId = m_ccData.iCustomerID;
        nfData.iSourceId = (int)NewsFeedSourceType.ICP_COURTESY_CALL;
        nfData.sxContent = String.Format(
            "Contact Person     : {0}<br/>"+
            "Realization Date   : {1}<br/>"+
            "Note               : {2}",
            m_ccData.sxContactPersonName,
            m_ccData.dtmRealizationDate.ToString("dd-MMMM-yyyy"),
            m_ccData.sxNote);
        nfData.sxTitle = "Courtesy Call has been realized";
        nfData.sxUserId_Poster = m_sxUID;
        nfData.sxUserName_Poster = m_sxUserName;
        DBQuery.NewsFeedTable.SaveNewsFeed(nfData);
        
        return m_iCCID.ToString();
    }
	private string HandleView(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        int iCPID = -1;
        bool isNewCP = sxSplitted[2].ToString().ToUpper().Equals("Y");
        switch (sxSplitted[0].ToUpper())
        {
            case "SHOW":
                iCPID = Convert.ToInt32(sxSplitted[1]);
                ContactInfoData cpInfoData = new ContactInfoData();
                Cached.ContactPersonTable cachedCP = new Cached.ContactPersonTable();
                cpInfoData = cachedCP.GetContactInfoData(iCPID, isNewCP);
                sxOutput = CCManager.BuildEditor_CPInfoPopUp(iCPID.ToString(), cpInfoData);
                break;
        }

        return sxOutput;
    }
    #endregion Custom Methods
}
